﻿using Microsoft.AspNetCore.Http;
using System;
using System.Text;

namespace Having.Site.Base
{
    public class Command
    {
        /// <summary>
        /// 匹配html并去除html标志
        /// </summary>
        /// <param name="str">要匹配的字符串</param>
        /// <returns></returns>
        public static string ReMoveHTML(string str)
        {
            System.Text.RegularExpressions.Regex Regex = new System.Text.RegularExpressions.Regex("<[^>]*>", System.Text.RegularExpressions.RegexOptions.Compiled);
            str = Regex.Replace(str, "");
            return str;
        }
        /// <summary>
        /// 匹配用户名和密码是否由指定长度位的数字＋字母＋_组成
        /// </summary>
        /// <param name="str">用户名或者密码(未加密)</param>
        /// <param name="maxLength">匹配的最大长度</param>
        /// <param name="minLength">匹配的最小长度</param>
        /// <returns>bool</returns>
        public static bool ChkPassWord(string str, int minLength, int maxLength)
        {
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"^[0-9a-zA-Z_]{" + minLength + "," + maxLength + "}$");
            System.Text.RegularExpressions.Match m = regex.Match(str);
            return m.Value.Length == str.Length ? true : false;
        }
        /// <summary>
        /// 判断用户名是否由英文数字组成的6-16位，并且第一位是英文
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static bool CheckUserName(string username)
        {
            System.Text.RegularExpressions.Regex Regex = new System.Text.RegularExpressions.Regex(@"^[A-Za-z]{1}[0-9A-Za-z]{5,15}$");
            return Regex.IsMatch(username);
        }

        /// <summary>
        /// 匹配email
        /// </summary>
        /// <param name="email">email</param>
        /// <returns>bool</returns>
        public static bool ChkEmail(string email)
        {
            System.Text.RegularExpressions.Regex Regex = new System.Text.RegularExpressions.Regex(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
            return Regex.IsMatch(email);
        }
        /// <summary>
        /// 匹配电话号码
        /// </summary>
        /// <param name="moblie"></param>
        /// <returns></returns>
        public static bool ChkMobile(string moblie)
        {
            System.Text.RegularExpressions.Regex Regex = new System.Text.RegularExpressions.Regex(@"^1(3|5|6|7|8|9)[0-9]{9}$");
            return Regex.IsMatch(moblie);
        }
        /// <summary>
        /// ip转换十进制长整型[ip4]
        /// </summary>
        /// <param name="ip">ip(201.5.102.1)</param>
        /// <returns></returns>
        public static long ConvertIPToDEC(string ip)
        {

            string[] arr = System.Text.RegularExpressions.Regex.Split(ip.Replace(".", ","), ",");
            long a = long.Parse(arr[0].ToString());
            long b = long.Parse(arr[1].ToString());
            long c = long.Parse(arr[2].ToString());
            long d = long.Parse(arr[3].ToString());
            long s = a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;
            return s;
        }
        /// <summary>
        /// 十进制长整型转换为ip [ip4]
        /// </summary>
        /// <param name="num">如(1034938398)</param>
        /// <returns></returns>
        public static string ConvertDECToIP(long num)
        {
            int a = (int)(num / (256 * 256 * 256));
            int b = (int)((num % (256 * 256 * 256)) / (256 * 256));
            int c = (int)(((num % (256 * 256 * 256)) % (256 * 256)) / 256);
            int d = (int)(((num % (256 * 256 * 256)) % (256 * 256)) % 256);
            string s = a.ToString() + "." + b.ToString() + "." + c.ToString() + "." + d.ToString();
            return s;
        }
        /// <summary>
        /// 将asc数字转换为字符
        /// </summary>
        /// <param name="acsnum">97-122之间(a-z), 65-90(A-Z),48-57(0-9)</param>
        /// <returns></returns>
        public static string GetASCToString(int acsnum)
        {
            return char.ConvertFromUtf32(acsnum);
        }
        /// <summary>
        /// 将字符转换为asc数字
        /// </summary>
        /// <param name="n">字符</param>
        /// <returns></returns>
        public static int GetStringToASC(char n)
        {
            return char.ConvertToUtf32(n.ToString(), 0);
        }
        /// <summary>
        /// 对url的参数进行编码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string UrlEnCode(string str)
        {
            return System.Web.HttpUtility.UrlEncode(str);
        }

        
    }
}
